//
// Created by denglibin on 2021/2/4.
// 纹理

#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
#define true 1
#define false 0
static const int WIN_WIDTH = 1366;
static const int WIN_HEIGHT = 768;
static SDL_Window *win; //窗口指针
static SDL_Surface  *winSurface;
static SDL_Renderer *winRenderer;
static int quit = 0;
static SDL_Texture *theTexture;

/**
 * 加载图片 返回纹理
 * @param file
 * @return 纹理
 */
static SDL_Texture* loadImage(const char * file){
    SDL_Surface* imgSurface = IMG_Load(file);
   // SDL_Texture * texture = IMG_LoadTexture(winRenderer, file);
   // 将指定颜色设为透明
    //SDL_SetColorKey(imgSurface,SDL_TRUE, SDL_MapRGB(imgSurface->format,255,255,255));
    SDL_Texture * texture = SDL_CreateTextureFromSurface(winRenderer, imgSurface);
    if(texture == NULL){
        printf("IMG_LoadTexture ERROR:%s\n", SDL_GetError());
        exit(1);
    }
    return texture;
}

/**
 * 创建并显示窗口
 */
static void createAndShowWin(){
    win = SDL_CreateWindow("demo2", SDL_WINDOWPOS_UNDEFINED,
                           SDL_WINDOWPOS_UNDEFINED,
                           WIN_WIDTH, WIN_HEIGHT,
                           SDL_WINDOW_SHOWN);
    if(win == NULL){
        printf("SDL_CreateWindow ERROR:%s\n", SDL_GetError());
        exit(1);
    }
    //获取窗口显示层
    winSurface = SDL_GetWindowSurface(win);

    SDL_SetSurfaceColorMod(winSurface, 255, 255, 255);

    //主窗口渲染器
    winRenderer = SDL_CreateRenderer(
            win,
            -1,  //序号 -1表示第一个
            SDL_RENDERER_ACCELERATED//使用硬件加速
            | SDL_RENDERER_PRESENTVSYNC); //和刷新频率同步
}

/**
 * 根据图片初始化纹理
 */
static void initTexture(){
    theTexture = loadImage("img/cartoon.bmp");
}
/**
 * 渲染图片
 */
static void showImage(const char *file, int  x, int y){
    SDL_Texture* texture =  loadImage(file); //纹理
    SDL_Rect  pos; //位置
    pos.x = x;
    pos.y = y;
    //获取纹理宽高
    SDL_QueryTexture(texture, NULL, NULL, &pos.w, &pos.h);
    //将纹理复制到渲染器上
    SDL_RenderCopy(winRenderer, //渲染器
                   texture,
                   NULL,
                   &pos);
    SDL_RenderPresent(winRenderer); //展示
}


/**
 * 关闭资源
 */
static void close(){
    // 清理资源
    SDL_DestroyTexture(theTexture);
    SDL_FreeSurface(winSurface);
    SDL_DestroyRenderer(winRenderer);
    SDL_DestroyWindow(win);
    SDL_Quit();
}
/**
 * 事件处理
 * @param e
 */
static void keyDownEvent(SDL_Event* e){
    switch (e->key.keysym.sym) {
        case SDLK_ESCAPE:
            quit = true;
            break;
        case SDLK_DOWN:
            break;
        case SDLK_UP:
            break;
        default:
            break;
    }

}

/**
 * 初始化
 */
static void init(){
   createAndShowWin();
   initTexture();
}
static void move(){

    int w,h; //纹理 宽高
    SDL_Rect srcPos; //要渲染的纹理的位置和宽高
    SDL_Rect dstPos; // 在渲染器上的位置

//获取纹理宽高
    SDL_QueryTexture(theTexture, NULL, NULL, &w, &h);
    dstPos.x = 0;
    dstPos.y = 0;
    dstPos.h = h;
    dstPos.w = w/8;
    for(int i = 0; i < 8; i++){
        srcPos.x = i * (w/8); //截取一帧
        srcPos.y = 0;
        srcPos.w = (w/8);
        srcPos.h = h;

        //dstPos.x += 5; //移动起来
        SDL_RenderClear(winRenderer);

        //将纹理复制到渲染器上
        SDL_RenderCopy(winRenderer, //渲染器
                       theTexture,
                       &srcPos,
                       &dstPos);
        SDL_RenderPresent(winRenderer); //展示
        SDL_Delay(100);
        if(i >=7){
            i = 0;
        }
    }
}
/**
 * 演示
 */
static void demo(){

    SDL_Thread *thread = SDL_CreateThread((SDL_ThreadFunction) move, "demo", NULL); //在单独的线程中执行
    if ( thread == NULL ) {
        fprintf(stderr, "无法创建线程： %s/n", SDL_GetError());
        return;
    }


}

/**
 * 事件循环
 */
static void eventLoop(){
    SDL_Event  event;
    while (!quit)
    {//主消息循环
        SDL_WaitEvent(&event);

        switch (event.type)
        {
            //用户从菜单要求退出程序
            case SDL_QUIT:
                quit = 1;
                break;
            case SDL_KEYDOWN:
                keyDownEvent(&event);
                break;
            default:
                break;
        }
    }
}

int main3(int argc, char* argv[]){

    init();
    demo();
    eventLoop();
    close();
    return 0;
}